package com.camus.verify.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.camus.framework.common.CommonResult;
import com.camus.utils.xls.EasyExcelUtils;
import com.camus.verify.biz.VerifyTaskBiz;
import com.camus.verify.entity.VerifyTask;
import com.camus.verify.vo.VerifyTaskApVO;
import com.camus.verify.vo.VerifyTaskInvoiceVO;

import cn.hutool.core.lang.Pair;

/**
 * <p>
 * 核销任务 前端控制器
 * </p>
 *
 * @author jie.deng
 * @since 2021-09-04
 */
@RestController
@RequestMapping("/verification/verify-task")
public class VerifyTaskController {

	private static final Logger log = LoggerFactory.getLogger(VerifyTaskController.class);

	@Autowired
	private VerifyTaskBiz verifyTaskBiz;
	@Autowired
	private ApplicationContext applicationContext;

	@RequestMapping("/submitVerifyTask")
	public Object submitVerifyTask(@RequestParam("uploadInvoiceFile") MultipartFile invoiceFile,
			@RequestParam("uploadOrderFile") MultipartFile apgFile) {
		VerifyTask verifyTask = null;
		try {
			// 读取发票xls数据
			final List<VerifyTaskInvoiceVO> invoiceList = new ArrayList<VerifyTaskInvoiceVO>();
			EasyExcelUtils.readExcel(invoiceFile.getInputStream(), invoiceList, VerifyTaskInvoiceVO.class);

			// 读取台账xls数据
			final List<VerifyTaskApVO> apgList = new ArrayList<VerifyTaskApVO>();
			EasyExcelUtils.readExcel(apgFile.getInputStream(), apgList, VerifyTaskApVO.class);

			// 相同本方公司+供应商分为同一组
			Map<Pair<String, String>, List<VerifyTaskInvoiceVO>> invoiceMap = invoiceList.stream()
					.collect(Collectors.groupingBy(t -> Pair.of(t.getLeName(), t.getTsTrName())));
			Map<Pair<String, String>, List<VerifyTaskApVO>> apgMap = apgList.stream()
					.collect(Collectors.groupingBy(t -> Pair.of(t.getLeName(), t.getTsTrName())));
			Set<Pair<String, String>> groupSet = invoiceMap.keySet();
			groupSet.retainAll(apgMap.keySet());
			for (Pair<String, String> group : groupSet) {
				// 提交核销任务
				verifyTask = verifyTaskBiz.submitVerifyTask(invoiceMap.get(group), apgMap.get(group));
				
				// 发布事件，触发执行核销任务
				applicationContext.publishEvent(verifyTask);
			}
		} catch (Exception e) {
			log.error("提交核销任务发生异常", e);
		}
		return CommonResult.succ(verifyTask);
	}

	@RequestMapping("/excuteVerifyTask")
	public Object excuteVerifyTask(@RequestParam("taskCode") String taskCode) {
		try {
			verifyTaskBiz.excuteVerifyTask(taskCode);
		} catch (Exception e) {
			log.error("执行核销任务发生异常", e);
		}
		return CommonResult.succ(null);
	}

}
